/*
 * @Author: lujiangbo knight_lujb@163.com
 * @Date: 2024-10-18 15:51:30
 * @LastEditors: lujiangbo knight_lujb@163.com
 * @LastEditTime: 2024-10-22 07:22:22
 * @Description: 
 */
import { useRouter } from "vue-router";
import request from "./request";
import { UrlPrefix } from "../config/const";
export const utils: Utils = {
    async getClipboardData() {
        // const el = document.createElement('textarea');
        // document.body.appendChild(el);
        // el.focus();
        // document.execCommand('paste');
        // const clipboardData = el.value || (window as any).clipboardData?.getData('Text') || await navigator?.clipboard?.readText();
        // document.body.removeChild(el);
        // console.log('getClipboardData text', clipboardData);

        // const clipboardInstance = new ClipboardItem(undefined, null)
        // console.log('clipboardInstance',clipboardInstance);

        const clipboardData = ''
        // clipboardInstance.getData('text/plain')
        return clipboardData;
    },
    goBack(router: any) {
        router.go(-1)
    },
    getJson(URL: string) {
        // const res: CommonType.ResDataType<any> = await axios.get(URL)
        // https://oss.quliye.com/ce/staging/IQ/DETAILS.json
        // const [_, postUrl] = URL.split('https://oss.quliye.com')
        // return request.get(`${UrlPrefix.json}${postUrl}`)
        return fetch(URL as string).then(res => res.json())
    },
    // 获取URI参数对象
    queryParse(url) {
        url = url.replace('/#', '');
        return Array.from(new URL(url).searchParams).reduce((cur: { [k: string]: string }, [key, value]) => {
            cur[key] = value;
            return cur;
        }, {});
    },

    judgeClient() {
        let u = navigator.userAgent;
        let isAndroid = u.indexOf("Android") > -1 || u.indexOf("Adr") > -1; //判断是否是android 终端
        let isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //判断是否是 iOS 终端
        if (isAndroid) {
            return "Android";
        } else if (isIOS) {
            return "IOS";
        } else {
            return "PC";
        }
    },
    isAndroid() {
        return this.judgeClient() === "Android";
    },
    isIos() {
        return this.judgeClient() === "IOS";
    },
    isQieApp() {
        const ua = (window as any)?.navigator?.userAgent?.toLowerCase() || ''
        return ua.includes('com.qiekj.qeuser')
    },
    /**
    * 调⽤ bridge ⽆回参
    * @param {string} name ⽅法名称
    * @param {object} params 参数
    */
    bridge(name, params = {}) {
        if (!this.isQieApp()) {
            console.log("不在 app 内");
            return
        }
        if (this.isAndroid()) {
            if (Object.keys(params).length) {
                (window as any).quekeApi &&
                    (window as any).quekeApi[name] &&
                    (window as any).quekeApi[name](JSON.stringify(params));
            } else {
                (window as any).quekeApi &&
                    (window as any).quekeApi[name] &&
                    (window as any).quekeApi[name]();
            }
        } else if (this.isIos()) {
            (window as any).webkit &&
                (window as any).webkit.messageHandlers &&
                (window as any).webkit.messageHandlers[name] &&
                (window as any).webkit.messageHandlers[name].postMessage &&
                (window as any).webkit.messageHandlers[name].postMessage(params);
        } else {
            console.log("不在 app 内");
        }
    }
}